#pragma once
#include <iostream>
#include <string>
#include <functional>
#include <pthread.h>

using func_t = std::function<void(std::string name)>;
class Thread
{
public:
    Thread(const std::string& name, func_t func)
    :_name(name)
    ,_func(func)
    {}
    static void* Run(void* rags)
    {
        Thread* self = static_cast<Thread*> (rags);
        std::cout <<  self->_name << " start" <<std::endl;
        self->_func(self->_name);//把当前线程的名字传给HandlerTask(std::string& name)函数
        return nullptr;
    }
    //线程开始执行任务
    bool Start()
    {
        int n = pthread_create(&_id, nullptr, Run, (void*)this);
        if(n == 0) return true;
        return false; 
    }
    //线程退出
    void Quit()
    { 
        pthread_cancel(_id); 
    }
    //等待线程
    void join()
    { 
        pthread_join(_id, nullptr); 
    }
private:
    std::string _name;//线程名
    func_t _func;
    pthread_t _id;//线程id
};
